# -*- coding: utf-8 -*-
'''
Created on 2011-11-21

@author: Administrator
'''
import struct
import os
from setting import *
import time
from pinyin import *
def myfloat(src):
    if len(src.strip())==0:return 0
    return float(src)
def str2date(src):
#    print src
    return datetime.datetime(int(src[:4]),int(src[4:6]),int(src[6:8]),9,0,0)

class FoxTrader():
    def __init__(self):
        self.reg=['SH','SZ','HK','NS']
        self.pinyin = Pinyin()

        pass
    def startwork(self,stocklist):
        self.updateregstocklist(stocklist)
        for reg in stocklist.keys():
            self.writedayfile(reg)
    def updateregstocklist(self,stocklist):
        for reg in stocklist.keys():
            stocklistlen=len(stocklist[reg].keys())
            print 'updatestocklist',reg,stocklistlen
            ####################################
            fname=os.path.join(foxtrader,'data',reg,'StkData.sif')
            header=[-1,260,1294272000,28800]
            
            ###########################
            f=open(fname,'wb')
            for h in header:
                f.write(struct.pack('i',h))
            f.write(struct.pack('i',stocklistlen))
            i=0
            for stock in sorted(stocklist[reg].keys()):
    #                print stock,stocklist[stock]
                f.write(struct.pack('12s',str(stock)))
#                print (stocklist[reg][stock]).decode('utf8').encode('gbk'),len((stocklist[reg][stock]))
                f.write(struct.pack('20s',((stocklist[reg][stock])).encode('gbk','ignore')))
#                f.write(struct.pack('24s',str(stock)))
##############################################
#                print stocklist[reg][stock],self.pinyin.get_pinyin(stocklist[reg][stock])
                f.write(struct.pack('24s',self.pinyin.get_pinyin(stocklist[reg][stock]).encode('gbk','ignore')))
#################################################################
#                for k in range(0,6):
#                    f.write(struct.pack('i',0))
                f.write(struct.pack('i',1092616192))            
                for k in range(0,51):
                    f.write(struct.pack('i',0))
                f.write(struct.pack('i',1))
                for k in range(52,110):
                    f.write(struct.pack('i',0))
            f.close()        
    def writedayfile(self,reg):    
        stocklist={}
        blocklist={}
        blockid=0
        for root,dirs,files in os.walk(rawcontentpath):
            for f in files:
                if f[:2]==reg:
                    p=f.find('.')
                    code=f[2:p]
                    fs=open(os.path.join(root,f),'r')
                    clist=fs.read().split('\n')
                    fs.close()
                    slist=[]
                    for c in clist:
                        a=c.split('"')
                        b=[]
#                        print f,len(a),a
                        if len(a)>4:
                            b.append(time.mktime(str2date(a[0]).timetuple()))
                            for i in range(1,6):
#                                print i
                                b.append(myfloat(a[i]))
                            b.append(0)
                            b.append(0)
                            slist.append(b)                    
                    stocklist[code]=slist
                    blocklist[code]=[blockid]
#                    print code,len(slist),len(slist)/1200
                    blockid=blockid+1
                    if len(slist)>1200:
                        for b in range(0,len(slist)/1200):
                            blocklist[code].append(blockid)
                            blockid=blockid+1
#                    break
        stocklen=len(stocklist.keys())
       
########################################################
        header=[-35,258,33,1200,8192,655392]
        fname=os.path.join(foxtrader,'data',reg,'day.hqd')
        f=open(fname,'wb')
        for p in header:        
            f.write(struct.pack("i",p))
        f.write(struct.pack("i",blockid))
        f.write(struct.pack("i",stocklen))
        ##########################################
        for code in sorted(stocklist.keys()):
            f.write(struct.pack('10s',str(code)))
#            print code,len(stocklist[code])
#            return
            f.write(struct.pack("i",len(stocklist[code])))
            e=33
            for b in blocklist[code]:
                f.write(struct.pack("h",b))
                e=e-1
            for i in range(0,e):
                f.write(struct.pack("h",-1))
        for i in range(stocklen,8192):
            for a in range(0,7):
                f.write(struct.pack("h",0))
            for a in range(0,33):
                f.write(struct.pack("h",-1))
        #############################################
        for code in sorted(stocklist.keys()):
            for c in stocklist[code]:
#                print c 
                f.write(struct.pack("i",c[0]))
                for r in range(1,8):
                    f.write(struct.pack("f",c[r]))
            daylen=len(stocklist[code])
            while daylen>1200:
                daylen=daylen-1200
            for r in range(daylen,1200):
                for r in range(0,8):
                    f.write(struct.pack("f",0))                
            ############################################            
        f.close()
    def readdayfile(self,reg):
        fname=os.path.join(foxtrader,'data',reg,'day.hqd')
        f=open(fname,'rb')
        content=f.read()
        f.close()
        contentlen=len(content)
        ip=4*6
        blocknum=struct.unpack("i",content[ip:ip+4])[0]
        ip=ip+4        
        stocknum=struct.unpack("i",content[ip:ip+4])[0]
        ip=ip+4
        blocklist={}
        stocklist={}
        for r in range(0,stocknum):             
            stockcode=struct.unpack("6s",content[ip:ip+6])[0]
            ip=ip+6+4
            onestocklen=struct.unpack("i",content[ip:ip+4])[0]
            ip=ip+4
            blist=[]
            bid=1
            bid=struct.unpack("h",content[ip:ip+2])[0]
            while bid>-1:                
                blist.append(bid)
                ip=ip+2
                bid=struct.unpack("h",content[ip:ip+2])[0]
                
            ip=ip+66-2*len(blist)
#            print stockcode,onestocklen,blist
            stocklist[stockcode]=blist
#        for r in range(stocknum,8192):
#            ip=ip+80
        ip=ip+80*(8192-  stocknum   )

        blockbytes=(contentlen-ip)/1200

        stockdata={}
        for stock in stocklist.keys():
#            print 'read,',stock
            stockdata[stock]=[]
            for block in stocklist[stock]:
                datastart=ip+block*1200*32
                oneblock=[]
                for k in range(0,1200):
                    oneday={}
                    onedaystart=datastart+k*32
                    onedate=struct.unpack("i",content[onedaystart:onedaystart+4])[0] 
                    if onedate>0:
                        oneday['open']=struct.unpack("f",content[onedaystart+4*1:onedaystart+4*2])[0]
                        oneday['high']=struct.unpack("f",content[onedaystart+4*2:onedaystart+4*3])[0]
                        oneday['low']=struct.unpack("f",content[onedaystart+4*3:onedaystart+4*4])[0]
                        oneday['close']=struct.unpack("f",content[onedaystart+4*4:onedaystart+4*5])[0]
                        oneday['vol']=struct.unpack("f",content[onedaystart+4*5:onedaystart+4*6])[0]                        
                        oneblock.append(oneday)
                stockdata[stock]= stockdata[stock]+oneblock
#        for stock in sorted(stockdata.keys()):
#            print stock,len(stockdata[stock])
        return stockdata
    def readHdayfile(self,reg):
        fname=os.path.join(foxtrader,'data',reg,'day.hqd')
        f=open(fname,'rb')
        content=f.read()
        f.close()
        logfile='c:\\dayfile.txt'
        log=open(logfile,'w')
        ip=28
        contentlen=len(content)        
        
        stocklistlen=struct.unpack("i",content[ip:ip+4])[0]
        ip=ip+4
        for r in range(0,stocklistlen):             
            print struct.unpack("5s",content[ip:ip+5])[0]
            ip=ip+6+4
            print struct.unpack("h",content[ip:ip+2])[0]
            ip=ip+2+2
            print struct.unpack("h",content[ip:ip+2])[0]
            ip=ip+2
            print struct.unpack("h",content[ip:ip+2])[0]
            ip=ip+2+62
        for r in range(stocklistlen,8192):
            ip=ip+80
#        stockdate=str(datetime.date.fromtimestamp(struct.unpack("i",content[ip:ip+4])[0]))
#        print stockdate
#        ip=ip+4
#        stockopen=str(struct.unpack("f",content[ip:ip+4])[0])
#        print stockopen
#        for e in range(0,(contentlen-ip)/32):
##            print '###########################'
#            stockdate=str(datetime.date.fromtimestamp(struct.unpack("i",content[ip:ip+4])[0]))
#            ip=ip+4
#            stockopen=str(struct.unpack("f",content[ip:ip+4])[0])
#            ip=ip+4
#            stockhigh=str(struct.unpack("f",content[ip:ip+4])[0])
#            ip=ip+4
#            stocklow=str(struct.unpack("f",content[ip:ip+4])[0])
#            ip=ip+4
#            stockclose=str(struct.unpack("f",content[ip:ip+4])[0])
#            ip=ip+4
#            stockvol=str(struct.unpack("f",content[ip:ip+4])[0])
#            ip=ip+4+4+4
#            stockopen=str(struct.unpack("f",f.read(4))[0])
#            log.write(stockdate+'|'+stockopen+'|'+stockhigh+'|'+stocklow+'|'+stockclose+'|'+stockvol)
#            log.write('\n')

        log.close()
        
    def readstocklistfile(self,reg):
        fname=os.path.join(foxtrader,'data',reg,'StkData.sif')
        f=open(fname,'rb')
#        c=f.read(32)
        print struct.unpack("i",f.read(4))
        print struct.unpack("i",f.read(4))
        print struct.unpack("i",f.read(4))
        print struct.unpack("i",f.read(4))
#        print struct.unpack("h",f.read(2))
#        print struct.unpack("h",f.read(2))
#        stocklistlen=len(stocklist)
#        for r in range(0,1):
#            data_id = struct.unpack("cccccccccc",f.read(10))
#            print data_id
#        for r in range(0,4):
#            data_id = struct.unpack("i",f.read(4))
#            print data_id[0]

if __name__ == '__main__':
    fd=FoxTrader()
    fd.readdayfile('SZ')
    
    pass